﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DAO;
using DTO;
namespace tmp_GUI
{
    public partial class TestTriggerForm : Form
    {
        StringBuilder ma_sach = new StringBuilder();
        StringBuilder ma_phieu = new StringBuilder();
        int pmsCount = 0;
        int ptsCount = 0;
        int pttCount = 0;
        int date = DateTime.Now.Day;
        public TestTriggerForm()
        {
            InitializeComponent();
        }
        private void ShowMaster()
        {
            dgvCuonSach.AutoGenerateColumns = true;
            dgvChitietPMS.AutoGenerateColumns = true;
            dgvPhieumuonsach.AutoGenerateColumns = true;
            dgvCuonSach.DataSource = CuonSachBUS.TimTatCaCuonSach();
            dgvChitietPMS.DataSource = ChiTietPhieuMuonSachBUS.TimTatCaChiTietPMS();
            dgvPhieumuonsach.DataSource = PhieuMuonSachBUS.TimTatCaPMS();
        }
        private bool CheckDateForStoredData(){
            DateTime temp = BillCount.GetDate();
            if (temp.Day == DateTime.Now.Day && temp.Month == DateTime.Now.Month && temp.Year == DateTime.Now.Year)
            {
                return true;
            }
            return false;
        }
        private void TestTriggerForm_Load(object sender, EventArgs e)
        {
            BillCount.WriteBillInfo(DateTime.Now, 1, 2, 3);
            try
            {
                cmbLoaisach.DataSource = LoaiSachBUS.TimTatCaLoaiSach();
                cmbLoaisach.ValueMember = "MaLoaiSach";
                cmbLoaisach.DisplayMember = "TenLoaiSach";

                cmbTuade.DataSource = TuaDeSachBUS.TimTuaDeSachBangLoaiSach(cmbLoaisach.SelectedValue.ToString());
                cmbTuade.ValueMember = "MaTuaDe";
                cmbTuade.DisplayMember = "TenTuaDe";

                dtpHantra.Value = DateTime.Now.Add(new TimeSpan(7, 0, 0, 0));

                if (CheckDateForStoredData())
                {
                    List<int> temp = BillCount.getCount(0);
                    pmsCount = temp[0];
                    ptsCount = temp[1];
                    pttCount = temp[2];
                    //MessageBox.Show("pmsCount = " + pmsCount.ToString() + "\nptsCount = " + ptsCount.ToString() + "\npttCount = " + pttCount.ToString());

                }

                ShowMaster();
            }
            catch (Exception ex)
            {

            }
        }

        private void cmbLoaisach_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbTuade.DataSource = TuaDeSachBUS.TimTuaDeSachBangLoaiSach(cmbLoaisach.SelectedValue.ToString());
            cmbTuade.ValueMember = "MaTuaDe";
            cmbTuade.DisplayMember = "TenTuaDe";
        }
        private String getMaSach(){
            ma_sach.Remove(0, ma_sach.Length);
            ma_sach.Append(cmbTuade.SelectedValue.ToString());
            ma_sach.Append(tbStt.IntValue.ToString().PadLeft(2, '0'));
            return ma_sach.ToString();
        }
        private String getMaPhieu(){
            // Truong hop phieu muon sach
            ma_phieu.Remove(0, ma_phieu.Length);
            ma_phieu.Append("PMS");
            ma_phieu.Append(dtpNgaymuon.Value.Day.ToString().PadLeft(2, '0'));
            ma_phieu.Append(dtpNgaymuon.Value.Month.ToString().PadLeft(2, '0'));
            ma_phieu.Append(dtpNgaymuon.Value.Year.ToString().Substring(2));
            ma_phieu.Append(pmsCount.ToString().PadLeft(3, '0'));
            return ma_phieu.ToString();
        }

        private void ShiftDateTimer_Tick(object sender, EventArgs e)
        {
            //MessageBox.Show(date.ToString() + "\n" + DateTime.Now.Day.ToString());
            if (DateTime.Now.Day.CompareTo(date) != 0)
            {
                date = DateTime.Now.Day;
                pmsCount = ptsCount = pttCount = 0;
                MessageBox.Show("Tadaaaaaaaaaaaaaaa!!!!!!!");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (cbAuto.Checked)
            {
                tbPMSId.Text = getMaPhieu();
            }
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            try
            {
                lvItemlist.Items.Add(new ListViewItem(getMaSach()));
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void BtnCommit_Click(object sender, EventArgs e)
        {
            if (lvItemlist.Items.Count > 0)
            {
                PhieuMuonSachDTO pms = new PhieuMuonSachDTO();
                pms.MaPM = tbPMSId.Text;
                pms.SoThe = tbSothe.Text;
                pms.NgayMuon = dtpNgaymuon.Value;
                pms.NgayTra = dtpHantra.Value;

                if (PhieuMuonSachBUS.ThemPMS(pms))
                {
                    List<ChiTietPhieuMuonSachDTO> dschitiet = new List<ChiTietPhieuMuonSachDTO>();
                    foreach (ListViewItem lvi in lvItemlist.Items)
                    {
                        dschitiet.Add(new ChiTietPhieuMuonSachDTO(tbPMSId.Text, lvi.Text));
                    }
                    foreach (ChiTietPhieuMuonSachDTO ctpms in dschitiet)
                    {
                        if (!ChiTietPhieuMuonSachBUS.ThemChiTietPMS(ctpms))
                        {
                            MessageBox.Show("Commit fail for the item id = " + ctpms.MaTaiLieu);
                        }
                        
                    }
                    pmsCount++;
                    ShowMaster();
                }
                else{
                    MessageBox.Show("Opsssssssssss!");
                    return;
                }
            }
        }

        private void TestTriggerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            BillCount.WriteBillInfo(DateTime.Now, pmsCount, ptsCount, pttCount);
        }
    }
}